function showhide(index, end) {
    if (index <= 4) {
        $(".iPrevSlide").hide();
        $(".prev").css("cursor", "default");
    } else {
        $(".iPrevSlide").show();
        $(".prev").css("cursor", "pointer");
    }
    if (index >= end-5) {
        $(".iNextSlide").hide();
        $(".next").css("cursor", "default");
    } else {
        $(".iNextSlide").show();
        $(".next").css("cursor", "pointer");
    }
}

$(document).ready(function () {
    $('#sys_slider_code').tinycarousel();
//    $('#slider_small_thumb').tinycarousel();
    $("#city_to_tourist").msDropdown();

    $('#city_to_tourist').change(function () {
        $('.loading').show();
        var id = $('#city_to_tourist').val();
        if (id == 0) {
            $(".allhotel").show();
        } else {
            $(".allhotel").hide();
            $(".city_" + id).show();
        }
        $('.loading').hide();
    });

    /*//Zoom in slide show
    $('.iFullScreen').bind('click', function (e) {

        // Prevents the default action to be triggered.
        e.preventDefault();

        // Triggering bPopup when click event is fired
        var content = $(this).attr('data-src');
        $('#full_' + content).bPopup();
    });*/

    //SLIDE
    var end = $("#size_order_imgs").attr("data-val");
    console.log(end);
    end = end - 1;
    var index = 0;
	showhide(index, end);
	$(".slide-desc").mouseover(function () {
    });
    /*.mouseout(function () {
     $(".prev").hide();
     $(".next").hide();
     });*/

    $(".thumb_icon").click(function () {
        var sl = $(this).attr("attr-key");
        $(".large-desc").hide();
        $("#large_" + sl).fadeIn(500);
        $("#caption_" + sl).show();
		$("#overview_" + sl).hide();
		$("#overview_" + sl).fadeIn(200);
//        $(".active").removeClass("active");
//        $(this).addClass("active");
//        index = parseInt(sl);
//        console.log(index);
//        showhide(index, end);
    });
    $(".next").on("click", function () {
        if (index < end-5) {
            index = parseInt(index + 5);
            showhide(index, end);
//            if (index % 5 == 0) {
                $(".thumb_icon").hide();
                indexx = index + 5;
                if (indexx < end) {
                    for (i = index; i < indexx; i++) {
                        $("#overview_" + i).fadeIn(500);
                    }
                } else {
                    endd = end - 4;
                    for (i = endd; i <= end; i++) {
                        $("#overview_" + i).fadeIn(500);
                    }
                }
//            }

//            $(".large-desc").hide();
//            $("#large_" + index).fadeIn(500);
//            $("#caption_" + index).show();
        }
//        $(".active").removeClass("active");
//        $("#overview_" + index).addClass("active");
//        console.log(index);
    });

    $(".prev").on("click", function () {
        if (index > 4) {
            index = parseInt(index - 5);
            showhide(index, end);
//            if (index % 5 == 4) {
                $(".thumb_icon").hide();
                indexx = index + 5;
                for (i = index; i <= indexx; i++) {
                    $("#overview_" + i).fadeIn(500);
                }
//            }

//            $(".large-desc").hide();
//            $("#large_" + index).fadeIn(500);
//            $("#caption_" + index).show();
        }
//        $(".active").removeClass("active");
//        $("#overview_" + index).addClass("active");
//        console.log(index);
    });

    //Gửi bình luận
    $(".add-comment-textarea").mouseout(function () {
        if ($(".add-comment-title").val() != '' && $(".add-comment-textarea").val() != '') {
            $(".btn-send-comment").css("opacity", "1").css("cursor", "pointer");
        } else {
            $(".btn-send-comment").css("opacity", "0.5").css("cursor", "default");
        }
    });

    $(".btn-send-comment").click(function () {
        hotel_id = $('#news_id').val();
        title = $(".add-comment-title").val();
        content = $(".add-comment-textarea").val();

        console.log(title, content, hotel_id);
        jQuery.post(WEB_DIR + "ajax.php?act=hotel_review&code=add_travel_review", {
            title:title,
            content:content,
            hotel_id:hotel_id
        }, function (msg) {
            if (msg == 1) {
                alert("Gửi bình luận thành công, nhận xét của bạn đang chờ được xét duyệt !");
                $(".add-comment").slideToggle(300);
            } else if (msg == 0) {
                /*alert("Lỗi :D");
                 $(".add-comment").slideToggle(300);*/
            } else if (msg == 2) {
                alert("Vui lòng đăng nhập để sử dụng tính năng này !")
            }
        });
    });

    //xem bình luận
    $(".fb_comment").addClass("active");
    $(".oka_comment").click(function(){
        $(".cm-onsite").show(300);
        $(".cm-fb-plugin").hide(300);
        $(".active").removeClass("active");
        $(".oka_comment").addClass("active");
    });
    $(".fb_comment").click(function(){
        $(".cm-onsite").hide(300);
        $(".cm-fb-plugin").show(300);
        $(".active").removeClass("active");
        $(".fb_comment").addClass("active");
    });

    hotel_id = $('#news_id').val();
    jQuery.post(WEB_DIR + "ajax.php?act=hotel_review&code=all_travel_review", {
        hotel_id:hotel_id
    }, function (review) {
        number_reviews = Object.keys(review).length;
        //Giới hạn số review
        if (number_reviews > 10) {
            number_reviews = 10;
        }
        for (i = 0; i < number_reviews; i++) {
            var time = review[i].created_time;
            var date = new Date(time * 1000);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            $('<div class="comment-item"><div class="comment-info"><strong>' + review[i].user_name + '</strong><span>|</span><span>' + review[i].address + '</span><span>-</span><span>' + day + '/' + month + '/' + year + '</span></div><div class="comment-content"><div class="quote"><h5>' + review[i].tittle + '</h5><p>' + review[i].content + '</p></div></div></div>').appendTo("#lst-comment");
        }
    }, "json");

    $(".view_more").click(function () {
        console.log("view");
        $(".view_more").hide();
        jQuery.post(WEB_DIR + "ajax.php?act=hotel_review&code=all_travel_review", {
            hotel_id:hotel_id
        }, function (review) {
            number_reviews = Object.keys(review).length;
            //Giới hạn số review
            if (number_reviews > 100) {
                number_reviews = 100;
            }
            for (i = 3; i < number_reviews; i++) {
                var date = new Date(review[i].created_time * 1000);
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var day = date.getDate();
                $('<div class="comment-item"><div class="comment-info"><strong>' + review[i].user_name + '</strong><span>|</span><span>' + review[i].address + '</span><span>-</span><span>' + day + '/' + month + '/' + year + '</span></div><div class="comment-content"><div class="quote"><h5>' + review[i].tittle + '</h5><p>' + review[i].content + '</p></div></div></div>').appendTo("#lst-comment");
            }
        }, "json");
    });

    var i = 0;
    $('#next-page-1').click(function(){
        loadTravel();
        return false;
    });

    $('#pre-page-1').click(function(){
        i = parseInt(i-8);
        loadTravel();
        return false;
    });


    function loadTravel(){
        var loading = $('#loading-1');
        var next1 = $('#next-page-1');
        var pre1 = $('#pre-page-1');
        loading.show();
        $.post(
            'ajax.php?act=travel&code=loadTravel',
            {load :i},
            function (data) {
                console.log(data);
                $('#lst-tourist-place-1').html(data.item);
                if(data.end == 1){
                    next1.hide();
                }else{
                    next1.show();
                }
                loading.hide();
            }, 'json'
        );
        i = parseInt(i+4);
        if(i > 3){
            pre1.show();
        }else{
            pre1.hide();
        }
    }

    var j = 0;
    $('#next-page-2').click(function(){
        loadTravelTwo();
        return false;
    });

    $('#pre-page-2').click(function(){
        j = parseInt(j-20);
        loadTravelTwo();
        return false;
    });

    function loadTravelTwo(){
        var loading = $('#loading-2');
        var next2 = $('#next-page-2');
        var pre2 = $('#pre-page-2');
        loading.show();
        $.post(
            'ajax.php?act=travel&code=loadTravelTwo',
            {load :j},
            function (data) {
                console.log(data);
                $('#lst-tourist-place-2').html(data.item);
                if(data.end == 1){
                    next2.hide();
                }else{
                    next2.show();
                }
                loading.hide();
            }, 'json'
        );
        j = parseInt(j+10);
        if(j > 9){
            pre2.show();
        }else{
            pre2.hide();
        }
    }
});